package com.dss.dsc.convert;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.dss.dsc.model.ContentsModel;

import dsc.model.Array;
import dsc.model.util.GefModelUtil;
import dsc.model.util.JsonConvert;
import dsc.model.view.SysForView;
import dsc.model.view.SysForView.Complete.Sys;
import dsc.model.view.SysForView.Complete.Link;
import dsc.model.view.SysForView.Complete;

public class Convert {

	public void selectFile() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension shellSize = new Dimension(400, 100);
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		shell.setText("convert");
		shell.setSize(shellSize.width, shellSize.height);
		shell.setLocation(screenSize.width / 2 - shellSize.width / 2,
				screenSize.height / 2 - shellSize.height / 2);

		Composite path = new Composite(shell, 0);
		path.setLayout(new FillLayout(SWT.VERTICAL));
		final Text text = new Text(path, 0);

		Composite buttons = new Composite(shell, 0);
		buttons.setLayout(new FillLayout(SWT.HORIZONTAL));
		Button select = new Button(buttons, 0);
		select.setText("S&elect File");
		select.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				FileDialog dialog = new FileDialog(e.display.getActiveShell());
				String file = dialog.open();
				if (file != null) {
					text.setText(file);
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		Button convert = new Button(buttons, 0);
		convert.setText("C&onvert File");
		convert.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				File file = new File(text.getText());
				if (file.exists() && file.isFile()) {
					String json = dscConvert2Json(file);
					String fileName = file.getName().substring(0,
							file.getName().lastIndexOf("."));
					try {
						File folder = new File("json");
						if (!folder.exists()) {
							folder.mkdir();
						}
						File targetFile = new File("json/" + fileName + ".json");
						if (!targetFile.exists()) {
							targetFile.createNewFile();
						}
						OutputStreamWriter write = new OutputStreamWriter(
								new FileOutputStream(targetFile), "UTF-8");
						BufferedWriter writer = new BufferedWriter(write);
						writer.write(json);
						writer.close();

					} catch (IOException ex) {
						ex.printStackTrace();
					}
				}

			}

			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		shell.open();

		while (!shell.isDisposed()) {
			if (display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	public String dscConvert2Json(File file) {
		GefModelUtil mUtil = GefModelUtil.instance();
		Object obj = mUtil.readFile(file.getPath());
		Sys sys = new Sys();
		Array<Link> links = new Array<Link>();
		mUtil.convertContentsModel((ContentsModel) obj, sys, links);
		Complete complete = new Complete();
		complete.link = links;
		if (sys.content != null) {
			complete.sys = sys;// .content.composite().subSys;
		}

		SysForView sysForView = new SysForView();
		sysForView.pick = SysForView.complete_id;
		sysForView.picked = complete;
		//
		return JsonConvert.instance().sysForView2Json(sysForView);
	}

	public void jsonConvert2Dsc() {

	}

	public static void main(String[] args) {
		Convert convert = new Convert();
		convert.selectFile();
	}
}
